﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;
namespace UYD_WCS_AS_AGV_Controller.AGV
{

    /// <summary>
    /// AGV状态可视化处理，绑定页面
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class AgvStatusObservable<T> : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        // 变量触发条件委托（旧值→新值）
        public Func<T, T, bool> TriggerCondition { get; set; }

        private T _value;
        public string Name { get; }

        public T Value
        {
            get => _value;
            set
            {
                if (!EqualityComparer<T>.Default.Equals(_value, value))
                {
                    T oldValue = _value;
                    _value = value;

                    // 触发属性变更通知（用于双向绑定）
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));

                    // 条件判断与触发回调
                    if (TriggerCondition?.Invoke(oldValue, value) == true)
                    {
                        ValueChangedCallback?.Invoke(Name, oldValue, value);
                    }
                }
            }
        }

        // 全局回调（变量名, 旧值, 新值）
        public Action<string, T, T> ValueChangedCallback { get; set; }

        public AgvStatusObservable(string name, T initialValue)
        {
            Name = name;
            _value = initialValue;
        }
    }
}
